﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Xml.Serialization;
using Seagull.Configuration.ConfigSaver;
using Seagull.Model;

namespace Seagull.Configuration
{
    public class Config
    {
        #region Static Accessor

        private static Config _instance;

        public static Config Instance
        {
            get
            {
                if(_instance == null)
                {
                    throw new InvalidOperationException("Trying to access Config before it is loaded.");
                }
                return _instance;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether to supress validation for loading config file.
        /// </summary>
        /// <value>
        ///   <c>true</c> if [supress validation]; otherwise, <c>false</c>.
        /// </value>
        public static bool SupressValidation { get; set; }

        #endregion

        #region Load & Save

        private static readonly IConfigSaver _configSaver = new XmlConfigSaver();

        /// <summary>
        /// 从文件加载配置并返回加载的配置。
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static Config Load(string filename)
        {
            return _instance = _configSaver.Load<Config>(filename);
        }

        /// <summary>
        /// 加载一个已经存在的配置并返回该配置。
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static Config Load(Config instance)
        {
            return _instance = instance;
        }

        public void Save(string filename)
        {
            _configSaver.Save(this, filename);
        }

        private static string _configFileName;

        public static string ConfigFileName
        {
            get
            {
                if(_configFileName == null)
                {
                    _configFileName = ConfigurationManager.AppSettings["ConfigFile"];
                }
                return _configFileName;
            }
            set { _configFileName = value; }
        }

        #endregion

        public Config()
        {
//            Channels = new List<ChannelInfo>();
            ChannelConfig = new ChannelConfig();
            PathConfig = new PathConfig();
            SmsAlertConfig = new SmsAlertConfig();
            UIConfig = new UIConfig();
            DiskSpaceMonitorConfig = new DiskSpaceMonitorConfig();
            DataReadConfig = new DataReadConfig();
            TaskConfig = new TaskConfig();
        }

        [XmlElement("目录设定")]
        public PathConfig PathConfig { get; set; }

        [XmlElement("短信报警设定")]
        public SmsAlertConfig SmsAlertConfig { get; set; }

        [XmlElement("用户界面设定")]
        public UIConfig UIConfig { get; set; }

        [XmlElement("磁盘空间监控设定")]
        public DiskSpaceMonitorConfig DiskSpaceMonitorConfig { get; set; }

        [XmlElement("数据读取设定")]
        public DataReadConfig DataReadConfig { get; set; }

        [XmlElement("任务设定")]
        public TaskConfig TaskConfig { get; set; }

        [XmlElement("通道设定")]
        public ChannelConfig ChannelConfig { get; set; }
    }
}
